//
//  HintTool.swift
//  dysaidao
//
//  Created by Penlon Kim on 2022/12/28.
//  Copyright © 2022 王锦发. All rights reserved.
//

import Foundation
import UIKit

final class HintTool {
    
    private var hintView: UIView?
    
    private static let shared = HintTool()
    
    /// 显示toast
    /// - Parameters:
    ///   - message: 消息
    ///   - duration: 时长
    static func toast(_ message: String?, duration: TimeInterval = 2.0) {
        guard let message = message, !message.isEmpty else { return }
        shared.showHintView(hintView: hintView(with: message), duration: duration)
    }
    
    /// 显示toast
    /// - Parameters:
    ///   - hintView: 显示的View
    ///   - duration: 时长
    private func showHintView(hintView: UIView, duration: TimeInterval) {
        guard let window = UIApplication.shared.delegate?.window else { return }
        guard self.hintView == nil else { return }
        
        window?.addSubview(hintView)
        window?.bringSubviewToFront(hintView)
        self.hintView = hintView
        
        DispatchQueue.main.asyncAfter(deadline: .now() + duration) { [weak self] in
            UIView.animate(withDuration: 0.5, animations: { [weak self] in
                self?.hintView?.alpha = 0.5
            }, completion: { finished in
                self?.hintView?.removeFromSuperview()
                self?.hintView = nil
            })
        }
    }
    
    /// 获取一个toastView
    /// - Parameter message: 消息
    /// - Returns: 弹出的toastView
    private static func hintView(with message: String) -> UIView {
        let minWidth = UIScreen.main.bounds.size.width * 0.4
        let maxWidth = UIScreen.main.bounds.size.width * 0.8
        let padding = 10.0
        let font = UIFont.systemFont(ofSize: 15)
        
        let messageSize = message.extSize(withBoundingSize: CGSize(width: maxWidth - 2 * padding, height: 0), font: font)
        
        let labelFrame = CGRect(x: 0, y: 0, width: CGFloat(ceilf(Float(messageSize.width))), height: CGFloat(ceilf(Float(messageSize.height))))
        let viewFrame = CGRect(x: 0, y: 0, width: max(minWidth, Double(messageSize.width) + padding * 2), height: Double(messageSize.height) + padding * 2)
        
        // 文字的背景
        let hintView = UIView()
        hintView.isUserInteractionEnabled = false
        hintView.backgroundColor = UIColor(white: 0, alpha: 0.7)
        hintView.layer.cornerRadius = 7
        hintView.layer.masksToBounds = true
        hintView.frame = viewFrame
        hintView.center = CGPoint(x: CGFloat(ceilf(Float(UIScreen.main.bounds.size.width * 0.5))), y: CGFloat(ceilf(Float(UIScreen.main.bounds.size.height - 100.0))))
        
        // 显示文字的Label
        let hintLabel = UILabel()
        hintView.addSubview(hintLabel)
        hintView.isUserInteractionEnabled = false
        hintLabel.text = message
        hintLabel.textColor = .white
        hintLabel.textAlignment = .center
        hintLabel.font = font
        hintLabel.preferredMaxLayoutWidth = messageSize.width
        hintLabel.numberOfLines = 0
        hintLabel.frame = labelFrame
        hintLabel.center = CGPoint(x: CGFloat(ceilf(Float(hintView.bounds.size.width * 0.5))), y: CGFloat(ceilf(Float(hintView.bounds.size.height * 0.5))))
        
        return hintView
    }
    
}

extension String {
    func extSize(withBoundingSize boundingSize: CGSize, font: UIFont) -> CGSize {
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        let attributes = [NSAttributedString.Key.font: font]
        let contentSize = self.boundingRect(with: boundingSize, options: option, attributes: attributes, context: nil).size
        return contentSize
    }
}
